package com.yidu.finance.filter;

import java.io.IOException;

import javax.servlet.Filter;
import javax.servlet.FilterChain;
import javax.servlet.ServletException;
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;
import javax.servlet.annotation.WebFilter;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import com.yidu.finance.model.Member;

/**
 * 
 * 描述: 用户过滤器
 * @author liandyao
 * @date 2019年9月25日 上午10:36:37
 * @version 1.0
 */
@WebFilter("/*")
public class UserFilter implements Filter {
	
	
	
 
	public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain)
			throws IOException, ServletException {
		
		HttpSession session = ((HttpServletRequest)request).getSession();
		Member member = (Member) session.getAttribute("member");
		String path = ((HttpServletRequest)request).getServletPath();
		System.out.println(path);
		//不要过滤登录注册页面
		if(path.indexOf("noLogin.jsp")>-1 
				|| path.indexOf("login.jsp")>-1 
				|| path.indexOf("regist.jsp")>-1
				|| path.indexOf("MemberServlet")>-1
				|| path.indexOf("ValicodeServlet")>-1
				|| path.indexOf("res")>-1){
			chain.doFilter(request, response);
			return ;
		}
			
		if(member==null){
			//request.getRequestDispatcher("noLogin.jsp").forward(request, response);
			((HttpServletResponse)response).sendRedirect("pages/member/login.jsp");
			return ;
		}
		
		chain.doFilter(request, response);
	}
 
}
